//
//  NSDictionary+Extensions.m
//  Dictionary
//
//  Created by chenyibo on 15/12/28.
//  Copyright © 2015年 chenyibo. All rights reserved.
//

#import "NSDictionary+Extensions.h"

@implementation NSDictionary (Extensions)

+ (NSDictionary *)safetyDictionaryWithKeysAndObjects:(id)firstObject, ... {
    if (firstObject) {
        NSMutableArray *argsArray = [NSMutableArray arrayWithCapacity:0];
        [argsArray addObject:firstObject];
        
        va_list argList;
        va_start(argList, firstObject);
        id arg = va_arg(argList, id);
        while (arg != nil) {
            [argsArray addObject:arg];
            arg = va_arg(argList, id);
        }
        va_end(argList);
        
        NSUInteger count = [argsArray count];
        if (count > 0) {
            if (count == 1) {
                // 只有一个参数，不能拼接成字典
#ifdef DEBUG
                NSAssert(false, @"只有一个参数!");
#endif
                return nil;
            } else {
                if (count % 2 != 0) {
#ifdef DEBUG
                    NSAssert(false, @"参数个数为奇数!");
#endif
                    // 如果参数个数为奇数（如3，5，7，...），则需要去掉最后一个参数，剩余的参数可成功拼接成一个字典
                    count -= 1;
                }
                
                // 字典拼接
                NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:count];
                for (int i = 0; i < count / 2; ++i) {
                    id key = [argsArray objectAtIndex:2 * i];
                    id object = [argsArray objectAtIndex:2 * i + 1];
                    [temp setObject:object forKey:key];
                }
                
                // 不能直接返回temp，防止有人把它当做NSMutableDictionary使用
                return [NSDictionary dictionaryWithDictionary:temp];
            }
        } else {
#ifdef DEBUG
            NSAssert(false, @"参数错误");
#endif
            return nil;
        }
    } else {
#ifdef DEBUG
        NSAssert(false, @"没有传入任何参数");
#endif
        return nil;
    }
}

@end

@implementation NSMutableDictionary (Extensions)

+ (NSMutableDictionary *)safetyDictionaryWithKeysAndObjects:(id)firstObject, ... {
    NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:0];
    if (firstObject) {
        NSMutableArray *argsArray = [NSMutableArray arrayWithCapacity:0];
        [argsArray addObject:firstObject];
        
        va_list argList;
        va_start(argList, firstObject);
        id arg = va_arg(argList, id);
        while (arg != nil) {
            [argsArray addObject:arg];
            arg = va_arg(argList, id);
        }
        va_end(argList);
        
        NSUInteger count = [argsArray count];
        if (count > 0) {
            if (count == 1) {
                // 只有一个参数，不能拼接成字典
#ifdef DEBUG
                NSAssert(false, @"只有一个参数!");
#endif
                return temp;
            } else {
                if (count % 2 != 0) {
#ifdef DEBUG
                    NSAssert(false, @"参数个数为奇数!");
#endif
                    // 如果参数个数为奇数（如3，5，7，...），则需要去掉最后一个参数，剩余的参数可成功拼接成一个字典
                    count -= 1;
                }
                
                // 字典拼接
                for (int i = 0; i < count / 2; ++i) {
                    id key = [argsArray objectAtIndex:2 * i];
                    id object = [argsArray objectAtIndex:2 * i + 1];
                    [temp setObject:object forKey:key];
                }
                
                return temp;
            }
        } else {
#ifdef DEBUG
            NSAssert(false, @"参数错误");
#endif
            return temp;
        }
    } else {
#ifdef DEBUG
        NSAssert(false, @"没有传入任何参数");
#endif
        return temp;
    }
}

- (void)setObject:(id)object forKey:(id<NSCopying>)key defaultValue:(id)value {
    if (key) {
        if (object) {
            [self setObject:object forKey:key];
        } else {
            if (value) {
                [self setObject:value forKey:key];
            }
        }
    }
}

@end
